package jiyun.com.yiyanmeng;


import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import adapter.LunTanJiYanAdapter;
import adapter.LunTanOffAdapter;
import adapter.LunTanTabAdapter;
import base.BaseFragment;
import bean.LunTanJiYanBean;
import bean.LunTanOffRecyBean;
import bean.LunTanSchoolTabBean;
import presenter.LunTanPresenter;
import utils.ToastUtil;
import view.LunTanView;


/**
 * A simple {@link Fragment} subclass.
 */
public class LunTanFragment extends BaseFragment<LunTanView, LunTanPresenter> implements LunTanView, View.OnClickListener {
    private View view;
    /**
     * 学校
     */
    private TextView mTvSchool;
    /**
     * 论坛
     */
    private TextView mTvLuntan;
    private RelativeLayout mRlBg;
    private ImageView mLetter;
    private TabLayout mTabSchool;
    /**
     * 更多
     */
    private TextView mTvMore;
    private ViewPager mVp;
    private LinearLayout mLlSchool;
    private LinearLayout mLLunTan;
    private ImageView mImageRelease;
    private LunTanTabAdapter tabAdapter;
    /**
     * 官方论坛
     */
    private TextView mTvOfficial;
    /**
     * 经验论坛
     */
    private TextView mTvExperience;
    private RecyclerView mRv1;
    private RecyclerView mRv2;
    private View line_official;
    private View line_experience;
    private List <LunTanOffRecyBean.InfoBean.PlateBean> plate;//官方论坛_平台
    private List <LunTanOffRecyBean.InfoBean.ArticleBean> article;//官方论坛_文章
    private LunTanOffAdapter offAdapter;

    private List <LunTanJiYanBean.InfoBean.TopBean> top;
    private List <LunTanJiYanBean.InfoBean.ArticleBean> bottom;//经验论坛
    private LunTanJiYanAdapter jiYanAdapter;

    public static LunTanFragment newInstance() {

        Bundle args = new Bundle();
        LunTanFragment fragment = new LunTanFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void showToast(String msg) {
        ToastUtil.show(getActivity(), msg);
    }

    @Override
    protected LunTanPresenter bindPresenter() {
        return new LunTanPresenter();
    }

    @Override
    protected void initData() {
        mPresenter.getData();
        mPresenter.getOffData();//官方论坛
        mPresenter.getJiYanData();//经验论坛
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initView(View view) {
        mTvSchool = (TextView) view.findViewById(R.id.tv_school);
        mTvSchool.setOnClickListener(this);
        mTvLuntan = (TextView) view.findViewById(R.id.tv_luntan);
        mTvLuntan.setOnClickListener(this);
        mRlBg = (RelativeLayout) view.findViewById(R.id.rl_bg);
        mLetter = (ImageView) view.findViewById(R.id.letter);
        mTabSchool = (TabLayout) view.findViewById(R.id.tab_school);
        mTvMore = (TextView) view.findViewById(R.id.tv_more);
        mVp = (ViewPager) view.findViewById(R.id.vp);
        mLlSchool = (LinearLayout) view.findViewById(R.id.ll_school);
        mLLunTan = (LinearLayout) view.findViewById(R.id.ll_luntan);
        mImageRelease = (ImageView) view.findViewById(R.id.image_release);
        mTvOfficial = (TextView) view.findViewById(R.id.tv_official);
        mTvOfficial.setOnClickListener(this);
        mTvExperience = (TextView) view.findViewById(R.id.tv_experience);
        mTvExperience.setOnClickListener(this);
        mRv1 = (RecyclerView) view.findViewById(R.id.rv1);
        mRv2 = (RecyclerView) view.findViewById(R.id.rv2);
        line_official=view.findViewById(R.id.line_official);
        line_experience=view.findViewById(R.id.line_experience);
        initOffRv1();
        initOffRv2();
    }

    private void initOffRv2() {
        //经验论坛
        mRv2.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRv2.addItemDecoration(new DividerItemDecoration(getActivity(),DividerItemDecoration.VERTICAL));
        top=new ArrayList <>();
        bottom=new ArrayList <>();
        jiYanAdapter = new LunTanJiYanAdapter(top, bottom, getActivity());
        mRv2.setAdapter(jiYanAdapter);
    }

    private void initOffRv1() {
        //官方论坛
        mRv1.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRv1.addItemDecoration(new DividerItemDecoration(getActivity(),DividerItemDecoration.VERTICAL));
        plate=new ArrayList <>();
        article=new ArrayList <>();
        offAdapter = new LunTanOffAdapter(plate, article, getActivity());
        mRv1.setAdapter(offAdapter);
    }

    @Override
    protected int bindLayout() {
        return R.layout.fragment_luntan;
    }


    //点击事件
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.tv_school://学校
                mLlSchool.setVisibility(View.VISIBLE);
                mLLunTan.setVisibility(View.GONE);
                mImageRelease.setVisibility(View.GONE);
                mRlBg.setBackground(getResources().getDrawable(R.mipmap.shequ_bg_1));
                mTvSchool.setTextColor(getResources().getColor(R.color.colorTheme));
                mTvLuntan.setTextColor(getResources().getColor(R.color.colorWhite));
                break;
            case R.id.tv_luntan://论坛
                mLLunTan.setVisibility(View.VISIBLE);
                mLlSchool.setVisibility(View.GONE);
                mImageRelease.setVisibility(View.GONE);
                mRlBg.setBackground(getResources().getDrawable(R.mipmap.shequ_bg));
                mTvLuntan.setTextColor(getResources().getColor(R.color.colorTheme));
                mTvSchool.setTextColor(getResources().getColor(R.color.colorWhite));
                break;
            case R.id.tv_official://官方论坛
                mRv2.setVisibility(View.GONE);
                mRv1.setVisibility(View.VISIBLE);

                mImageRelease.setVisibility(View.GONE);
                mTvOfficial.setTextColor(getResources().getColor(R.color.colorTheme));
                line_official.setVisibility(View.VISIBLE);
                mTvExperience.setTextColor(getResources().getColor(R.color.colorgraya));
                line_experience.setVisibility(View.GONE);
                break;
            case R.id.tv_experience://经验论坛
                mRv2.setVisibility(View.VISIBLE);
                mRv1.setVisibility(View.GONE);

                mImageRelease.setVisibility(View.VISIBLE);
                mTvExperience.setTextColor(getResources().getColor(R.color.colorTheme));
                line_experience.setVisibility(View.VISIBLE);
                mTvOfficial.setTextColor(getResources().getColor(R.color.colorgraya));
                line_official.setVisibility(View.GONE);
                break;
        }
    }


    @Override
    public void onSuccess(LunTanSchoolTabBean lunTanSchoolTabBean) {
        List <LunTanSchoolTabBean.InfoBean> tablist = lunTanSchoolTabBean.getInfo();
        ArrayList <String> list = new ArrayList <>();
        ArrayList <BaseFragment> fragments = new ArrayList <>();
        for (int i = 0; i < tablist.size(); i++) {
            list.add(tablist.get(i).getName());
            fragments.add(LunTanNewsFragment.getInstance(Integer.parseInt(tablist.get(i).getId())));
        }
        tabAdapter = new LunTanTabAdapter(getChildFragmentManager(), list, fragments);
        mVp.setAdapter(tabAdapter);
        mTabSchool.setupWithViewPager(mVp);
    }

    @Override
    public void onFail(String error) {
        ToastUtil.show(getActivity(), error);
    }

    @Override
    public void setOffSuccess(LunTanOffRecyBean lunTanOffRecyBean) {
        plate.addAll(lunTanOffRecyBean.getInfo().getPlate());
        article.addAll(lunTanOffRecyBean.getInfo().getArticle());
        offAdapter.notifyDataSetChanged();
    }

    @Override
    public void setJiYanSuccess(LunTanJiYanBean lunTanJiYanBean) {
        top.addAll(lunTanJiYanBean.getInfo().getTop());
        bottom.addAll(lunTanJiYanBean.getInfo().getArticle());
        jiYanAdapter.notifyDataSetChanged();
    }


}
